package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.mapper.UserRoleMapper;
import com.zzyl.service.RoleService;
import com.zzyl.utils.EmptyUtil;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;


@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleResourceMapper roleResourceMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public PageResponse<RoleVo> findRolePage(RoleDto roleDto, int pageNum, int pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        Page<List<Role>> page = roleMapper.selectList(roleDto);
        PageResponse<RoleVo> roleVoPageResponse = PageResponse.of(page, RoleVo.class);
        return roleVoPageResponse;
    }

    @Override
    public void createRole(RoleDto roleDto) {
        //转换
        Role role = BeanUtil.toBean(roleDto, Role.class);
        roleMapper.insert(role);

    }

    /*
    * 查询角色所拥有的资源
     */
    @Override
    public Set<String> findCheckResource(Long roleId) {

        return roleResourceMapper.selectResourceNoByRoleId(roleId);
    }

    /**
     * 角色修改
     * @param roleDto
     * @return
     */
    @Override
    @Transactional
    public Boolean updateRole(RoleDto roleDto) {
        //1.对象拷贝
        Role role = BeanUtil.toBean(roleDto, Role.class);
        //2.修改角色
        roleMapper.updateByPrimaryKeySelective(role);
        //3.判断是否修改角色对应的资源数据
        if(ObjectUtil.isEmpty(roleDto.getCheckedResourceNos())){
            return true;
        }
        //4.删除角色对应的资源数据
        roleResourceMapper.deleteRoleResourceByRoleId(role.getId());
        //5.保存角色对应的资源数据
        List<RoleResource> roleResourcesList = new ArrayList<>();
        Arrays.asList(roleDto.getCheckedResourceNos()).forEach(n -> {
            RoleResource roleResource = RoleResource.builder()
                    .roleId(role.getId())
                    .resourceNo(n)
                    .dataState(SuperConstant.DATA_STATE_0)
                    .build();
            roleResourcesList.add(roleResource);
        });
        //6.如果集合为空，则返回true
        if (EmptyUtil.isNullOrEmpty(roleResourcesList)){
            return true;
        }
        //7.批量保存角色对应的资源数据
        roleResourceMapper.batchInsert(roleResourcesList);
        return null;
    }

    /**
     * 删除角色
     * @param roleId
     */
    @Override
    public void deleteById(Long roleId) {
        //删除角色与菜单关联
        roleResourceMapper.deleteRoleResourceByRoleId(roleId);
        //删除角色
        roleMapper.deleteByPrimaryKey(roleId);

    }

    @Override
    public List<RoleVo> findRoleVoListInUserId(List<Long> userIds) {
        return roleMapper.findRoleVoListInUserId(userIds);
    }

    @Override
    public List<RoleVo> getRoleList() {
        List<Role> roleList = roleMapper.selectBySta(SuperConstant.DATA_STATE_0);
        return BeanUtil.copyToList(roleList, RoleVo.class);
    }



}
